package com.juzhencms.apps.busi.web;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.busi.entity.AdminLoginUser;
import com.juzhencms.apps.busi.service.OrganizationService;

import lombok.extern.slf4j.Slf4j;

import com.juzhencms.apps.base.common.database.DBTable;
import com.juzhencms.apps.base.common.util.MD5Util;
import com.juzhencms.apps.base.common.util.StringUtil;
import com.juzhencms.apps.base.common.util.WebUtil;


@Controller
@RequestMapping("/checkRecord")
@Slf4j
public class CheckRecordController  extends BaseController{
	@Autowired
	private OrganizationService organizationService;
	//查询所有用户
	@RequestMapping(value="/gridlistData",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String gridlistData(HttpServletRequest request){
		Map data=new HashMap();
		List whereParamList=new ArrayList();
		String where="where del_flag=0 ";
		//显示所属机构下的数据
		if(this.getLoginUser().getSubOrgIds()!=null && !this.getLoginUser().getSubOrgIds().equals("")) {
			where+=" and check_org_id in ("+this.getLoginUser().getSubOrgIds()+") ";
		}
		
		
		
		//搜索参数
		String searchVar=null;
		String varValue=null;
		if(request.getParameter("varValue")!=null && !request.getParameter("varValue").toString().trim().equals("")){
			
			if(request.getParameter("searchVar")!=null && !request.getParameter("searchVar").toString().trim().equals("")){
				searchVar=request.getParameter("searchVar");
				if("create_user".equals(searchVar)){
					varValue=request.getParameter("varValue");
					Map record=this.getCommonService().getJdbcDao().queryRow("select id from "+DBTable.user+" where user=?",new Object[]{varValue});	
					if(record != null){
						where += " and create_user = " + record.get("id");
					}else{
						data.put("data", "");
						return WebUtil.resultString(data,request);
					}
				}else{
					whereParamList.add("%"+request.getParameter("varValue")+"%");
					where += " and "+searchVar+" like ? ";
				}
				
			}else{
				where+=" and data_title like ? ";
			}
		}

		//机构筛选条件
		if( StringUtil.isNotNullOrEmpty(request.getParameter("submit_org_id").toString().trim()) && !request.getParameter("submit_org_id").equals("0")){
			where+=" and submit_org_id=? ";
			whereParamList.add(request.getParameter("submit_org_id"));
		}
		if(request.getParameter("submit_site_id")!=null && !request.getParameter("submit_site_id").toString().trim().equals("")){
			where+=" and submit_site_id = ? ";
			whereParamList.add(request.getParameter("submit_site_id"));
		}
		
		if(request.getParameter("check_status")!=null && !request.getParameter("check_status").toString().trim().equals("")){
			where+=" and check_status = ? ";
			whereParamList.add(request.getParameter("check_status"));
		}
		if(request.getParameter("data_type")!=null && !request.getParameter("data_type").toString().trim().equals("")){
			where+=" and data_type = ? ";
			whereParamList.add(request.getParameter("data_type"));
		}
		//若选择了由我审批，则显示当前用户角色可以审批的数据
		if(request.getParameter("myCheck")!=null && request.getParameter("myCheck").toString().trim().equals("true")){
			if(this.getLoginUser().getRoleIdList()!=null && this.getLoginUser().getRoleIdList().size()>0) {
				String myRoleIds=org.apache.commons.lang3.StringUtils.join(this.getLoginUser().getRoleIdList().toArray(),",");
				where+=" and check_role_id in ("+myRoleIds+") ";
				
			}else {
				where+=" and 0 ";
			}
			
			
		}
		
		//分页处理
		int pageIndex=1;
		if(request.getParameter("pageIndex")!=null && !request.getParameter("pageIndex").toString().trim().equals("")){
			pageIndex=new Integer(request.getParameter("pageIndex")).intValue();
		}
		int pageSize=10;
		if(request.getParameter("pageSize")!=null && !request.getParameter("pageSize").toString().trim().equals("")){
			pageSize=new Integer(request.getParameter("pageSize")).intValue();
		}
		int start=0;
		if(pageIndex>1){
			start=(pageIndex-1)*pageSize;
		}
		whereParamList.add(start);
		whereParamList.add(pageSize);
		
		//排序参数 
		String sortField="create_time";
		String sortOrder="desc";
		if(request.getParameter("sortField")!=null && !request.getParameter("sortField").equals(""))
			sortField = request.getParameter("sortField");
		if(request.getParameter("sortOrder")!=null && !request.getParameter("sortOrder").equals(""))
			sortOrder = request.getParameter("sortOrder");
		
		//查询列表数据
		List dataList=this.getCommonService().getJdbcDao().querySql("select id,check_rule_id,check_rule_item_id,check_role_id,check_role_name,check_org_id,check_org_name,data_id,data_title,data_type,submit_org_id,submit_org_name,check_status,submit_user_name,DATE_FORMAT(submit_time,'%Y-%m-%d %H:%i:%s') as submit_time,DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') as create_time,data_history_id from "+DBTable.checkRecord+" "+where+" order by "+sortField+" "+sortOrder+",id desc limit ?,?", whereParamList.toArray());
		if(dataList!=null && dataList.size()>0){
			//判断当前用户是否具备审批权限	
			for(int i=0;i<dataList.size();i++){
				Map row=(Map)dataList.get(i);
				if(this.getLoginUser().getRoleIdList()!=null && this.getLoginUser().getRoleIdList().contains(row.get("check_role_id"))) {
					row.put("check_enable", 1);
				}
			}
		}
		
		//查询总条数
		Map countRow=this.getCommonService().getJdbcDao().queryRow("select count(id) as count_num from "+DBTable.checkRecord+" "+where, whereParamList.subList(0, whereParamList.size()-2).toArray());
		int recordsTotal=0;
		if(countRow.get("count_num")!=null){
			recordsTotal=new Integer(countRow.get("count_num").toString()).intValue();
		}
		
		//拼装返回的数据
		data.put("data", dataList.toArray());
		data.put("itemsCount", recordsTotal);
		return WebUtil.resultString(data,request);
	}

}
